package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.cache.service.IScenicCommentStaticVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.ScenicCommentStaticVO;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.repository.ScenicCommentRepository;
import cn.wolfcode.luowowo.common.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ScenicCommentStaticVOServiceImpl implements IScenicCommentStaticVOService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ScenicCommentRepository scenicCommentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;


    public boolean commentThumbUp(Long userId, String sid) {
        boolean flag=false;
        String idkey = RedisKeys.USER_STATIC_SCENIC_THUM.join(userId.toString());
        String vokey = RedisKeys.SCENIC_COMMENT_VO.join(sid.toString());
        ScenicCommentStaticVO scenicCommentStaticVO = this.getScenicCommentStaticVo(sid);
        List<String> list=null;
        if (stringRedisTemplate.hasKey(idkey)){
            String listStr=stringRedisTemplate.opsForValue().get(idkey);
            list=JSON.parseArray(listStr,String.class);
        }else {
            list=new ArrayList<>();
        }
        //判断是否包含当前游记的id
        if (list.contains(sid)){//如果包含就是取消收藏操作
            scenicCommentStaticVO.setThumbsupnum(scenicCommentStaticVO.getThumbsupnum()-1);
            list.remove(sid);
        }else {
            scenicCommentStaticVO.setThumbsupnum(scenicCommentStaticVO.getThumbsupnum()+1);
            list.add(sid);
            flag=true;
        }
        //更新redis中的用户收藏的游记id 列表,并且更新游记vo对象
        stringRedisTemplate.opsForValue().set(idkey,JSON.toJSONString(list));
        stringRedisTemplate.opsForValue().set(vokey,JSON.toJSONString(scenicCommentStaticVO));

        return flag;
    }


    public ScenicCommentStaticVO getScenicCommentStaticVo(String sid) {
        //1.首先要判断这篇攻略所对应的vo对象是否在redis存在
        String key = RedisKeys.SCENIC_COMMENT_VO.join(sid);
        ScenicCommentStaticVO scenicCommentStaticVO=null;
        if (stringRedisTemplate.hasKey(key)){
            //从redis获取到值
            String voStr = stringRedisTemplate.opsForValue().get(key);
            scenicCommentStaticVO = JSON.parseObject(voStr, ScenicCommentStaticVO.class);
        }else {
            scenicCommentStaticVO = new ScenicCommentStaticVO();
            //然后查询这篇攻略的统计字段的数值.再存储到redis中
            /*Scenic scenic = scenicService.get(id);*/
            //利用@BeanUtils工具类将StrategyDetail游记明细对象中的属性值拷贝到和VO对象中,只有属性名相同的才会拷贝过去
            ScenicComment scenicComment = scenicCommentRepository.findById(sid).get();
            scenicCommentStaticVO.setThumbsupnum(scenicComment.getThumbupnum());
            scenicCommentStaticVO.setScenicCommentId(scenicComment.getId());
            scenicCommentStaticVO.setUserId(scenicComment.getUserId());
        }
        //最后更新redis中的vo
        stringRedisTemplate.opsForValue().set(key,JSON.toJSONString(scenicCommentStaticVO));
        return scenicCommentStaticVO;
    }


}
